package com.zcl.protocol;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;

import com.netqin.cache.DataCache;
import com.netqin.ui.share.sinaweibo.AsyncWeiboRunner;
import com.netqin.ui.share.sinaweibo.Utility;
import com.netqin.ui.share.sinaweibo.Weibo;
import com.netqin.ui.share.sinaweibo.WeiboParameters;
import com.zcl.bean.Task;
import com.zcl.bean.UserInfo;
import com.zcl.logic.MainService;
import com.zcl.logic.UserInfoService;
import com.zcl.utils.NqUtil;
import com.zcl.utils.Value;

public class APPProUser extends APPProBase {
	
	public APPProUser(int reqinfo, Context context, Object obj) {
		super(reqinfo, context, obj);
	}
	
	@Override
	public boolean generateRequestData(Context context) {
		if (mObject instanceof UserInfo) {
			UserInfo info = (UserInfo)mObject;
			WeiboParameters bundle = new WeiboParameters();
			bundle.add(ACCESS_TOKEN_PARAM, info.getToken());
			bundle.add(UID_PARAM, info.getUserId());
			AsyncWeiboRunner weiboRunner = new AsyncWeiboRunner(Weibo.getInstance());
			weiboRunner.request(context, REQUEST_SHOW_USERS, bundle, Utility.HTTPMETHOD_GET, mRequestListener);
			return true;
		}
		return false;

	}

	@Override
	public boolean parseResponseData(Context context, String source) {
		JSONObject json;
		UserInfo info = null;
		String imgUrlPath = "";
		boolean blog = false;
		try {
			json = new JSONObject(source);
			String userId = json.getString("id");
			String userName = json.getString("screen_name");
			imgUrlPath = json.getString("profile_image_url");
			String token = Weibo.getInstance().getAccessToken().getToken();
			String tokenSecret = token;
			String isDefualt = "0";
			info = new UserInfo(userId, userName, token, tokenSecret, isDefualt);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (info!=null) {
			
			// 获取用户头像
			String imgFilePath="";
			if (!TextUtils.isEmpty(imgUrlPath)) {
				imgFilePath= DataCache.getInstance(mContext.getApplicationContext()).getData(imgUrlPath);
			}
			if(TextUtils.isEmpty(imgFilePath)){
				Task t = new Task();
				Map<String, Object> taskParams = new HashMap<String, Object>();
				taskParams.put(Value.REQUEST_HEAD_ICON_ID, Integer.valueOf(info.getUserId()));
				taskParams.put(Value.REQUEST_HEAD_ICON_URLPATH, imgUrlPath);
		    	t.setTaskId(Task.REQUEST_NETWORK_HEADICON);
		    	t.setTaskParams(taskParams);
		    	MainService.newTask(t);
			}else {
				int imageViewBianchang = NqUtil.convertDIP2PX(mContext, 40);// 头像的边长（四方型）
				Bitmap bitmap = NqUtil.getBitmap(imgFilePath, imageViewBianchang, imageViewBianchang);
				if (bitmap!=null) {
					info.setUserIcon(new BitmapDrawable(mContext.getResources(), bitmap));
				}
			}
			
			blog = insertData(context, info);
		}
		return blog;
	}
	
	private boolean insertData(Context context, UserInfo info)
	{
		boolean blog = false;
		if (info!=null) {
			UserInfoService infoService = new UserInfoService(context);
			UserInfo userInfo = infoService.getUserInfoByUserId(info.getUserId());
			if (userInfo == null) {
				blog = infoService.insertUserInfo(info)>0;
			}else {
				blog = infoService.updateUserInfo(info, userInfo.getId())>0;
			}
		}
		
		return blog;
	}
}
